﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
namespace Gifplayer
{
    public partial class Form1 : Form
    {
        string[] args = null;
        string path = "";
        public Form1()
        {
            InitializeComponent();
            
        }
        public Form1(string[] args)
        {
            InitializeComponent();
            this.args = args;
        }
        void image_OnFrameChanged(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void Form1_Load(object sender, EventArgs e)
        { 
            if(args==null||args.Length==0)
            {
                if(path=="")
                {
                   if( openFileDialog1.ShowDialog()==DialogResult.OK)
                    {
                        path = openFileDialog1.FileName;
                        OpenImage();
                    }
                }
                else
                {
                    OpenImage();
                }
            }
            else
            {
                path = args[0];
                OpenImage();
            }
            
            
     
        }
        Image[] images = null;
        private void Findfiles(string s)
        {
            ArrayList imagelist = new ArrayList();
            string dir = Path.GetDirectoryName(s);
            string[] files = Directory.GetFiles(dir);
            filelist.Clear();
            filelist.LargeImageList = null;

            foreach (string file in files)
            {
                if(CheckExt(file))
                {
                    imagelist.Add(file);
                   // Path.GetFileName(file);
                    logbox_output(file+"加入文件列表");
                    
                }
            }
            if (filelist.LargeImageList == null)
            {
                filelist.LargeImageList = new ImageList();
                filelist.LargeImageList.ImageSize = new Size(170, 100);
            }
            images = new Image[imagelist.Count];
          
            for(int i=0;i<images.Length;i++)
            {
                images[i] = Image.FromFile((string)imagelist[i]);
                
                filelist.LargeImageList.Images.Add(images[i]);
                filelist.Items.Add(Path.GetFileName((string)imagelist[i]),i);
            }


            
            
        }

        private void logbox_output(string s)
        {
            logbox.AppendText(s);
            logbox.AppendText(Environment.NewLine);
        }

        /// <summary>
        /// 检查文件后缀，判断是否为图像文件
        /// </summary>
        /// <param name="s">文件路径</param>
        /// <returns></returns>
        private bool CheckExt(string s)
        {
            switch (Path.GetExtension(s))
            {
                case ".jpg":
                    return true;
                        
                case ".png":
                    return true;
                case ".tga":
                    return true;
                case ".bmp":
                    return true;
                case ".gif":
                    return true;
                default:
                    logbox_output(s + "不是图像文件");
                    return false;
                    
            }

        }
        private void OpenImage()
        {
            if(CheckExt(path))
            {
                pictureBox1.Image = Image.FromFile(path);
                Findfiles(path);
            }
            
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.pictureBox1.Image = pictureBox1.Image;
            f2.Show();
        }

        private void logbox_TextChanged(object sender, EventArgs e)
        {
            logbox.ScrollToCaret();
            logbox.Select(logbox.TextLength, 0);
        }

      

        private void filelist_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (filelist.SelectedItems.Count != 0)
            {
                if (images != null)
                    pictureBox1.Image = images[filelist.SelectedIndices[0]];
            }
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.Left||e.KeyCode==Keys.Down)
            {
                filelist.SelectedIndices.Clear();
                filelist.SelectedIndices.Add(2);
            }
            else if(e.KeyCode==Keys.Right||e.KeyCode==Keys.Up)
            {

            }
        }

        private void openfile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                path = openFileDialog1.FileName;
                OpenImage();
            }
        }

        private void close_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void contextMenuStrip2_Opening(object sender, CancelEventArgs e)
        {

        }

        private void clear_Click(object sender, EventArgs e)
        {
            logbox.Clear();
        }

        private void pictureBox1_DragEnter(object sender, DragEventArgs e)
        {
           
        }

        private void pictureBox1_DragDrop(object sender, DragEventArgs e)
        {
            

        }

        private void filelist_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void filelist_DragDrop(object sender, DragEventArgs e)
        {
            String[] files = e.Data.GetData(DataFormats.FileDrop, false) as String[];

            //Copy file from external application
            path = files[0];
            OpenImage();
        
        }
    }
}
